package main

import (
	"flag"
	"fmt"
	"os"
	"strconv"
)

func main() {
	serverFlag := flag.Bool("s", false, "以服务端运行")
	clientFlag := flag.Bool("c", false, "以客户端运行")
	networkString := flag.String("n", "100", "网速,单位Mb/s, 默认100M, 可以为10-800的整数")
	filePathString := flag.String("f", "", "文件名")

	flag.Parse()
	filePath = *filePathString
	var err error
	networkSpeed, err = strconv.Atoi(*networkString)
	if err != nil {
		fmt.Println("错误: 网速参数格式不正确, 必须为整数")
		flag.Usage()
		os.Exit(-1)
	}
	if networkSpeed > 800 {
		networkSpeed = 800
	}
	if networkSpeed < 10 {
		networkSpeed = 10
	}

	fmt.Println(copyString)
	fmt.Println("(若机器性能差, 请降低设定的网速，设置为-n 100运行)")
	fmt.Println("----------------------------------------------")

	// 检查是否同时指定了-s和-c
	if *serverFlag && *clientFlag {
		fmt.Println("错误: -s和-c不能同时存在")
		flag.Usage()
		os.Exit(-1)
	}

	if *serverFlag {
		fileInfo, err := os.Stat(filePath)
		if err != nil {
			fmt.Fprintf(os.Stdout, "文件%s不存在!\n", filePath)
			flag.Usage()
			os.Exit(-1)
		}
		fileName = fileInfo.Name()
		fileLength = fileInfo.Size()
		if fileLength > maxFilelength {
			fmt.Println("文件长度最大不超过32GB!")
			os.Exit(-1)
		}
		totalSlice = fileLength / sliceLength
		if fileLength%sliceLength > 0 {
			totalSlice++
		}
		setupLogFile("trans_server.log")
		server()
	} else if *clientFlag {
		setupLogFile("trans_client.log")
		client()
	} else {
		flag.Usage()
	}
}
